package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.AddTaskResultData;
import com.jzg.jcptadmin.data.vo.UploadVideoData;
import com.jzg.jcptadmin.viewinterface.AddTaskSubmitInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/11.
 */
public class AddTaskSubmitPresenter extends BasePresenter <AddTaskSubmitInterface>{
    private DataManager dataManager;
    private AddTaskSubmitInterface mView;
    private Subscription mSubscription;

    public AddTaskSubmitPresenter(DataManager dataManager){

        this.dataManager = dataManager;

    }

    @Override
    public void attachView(AddTaskSubmitInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void KsSubmitData(final Map<String,String> map){

        mSubscription =  dataManager.KsSubmitData(map).
                observeOn(AndroidSchedulers.mainThread()).
                subscribeOn(Schedulers.io()).subscribe(new Observer<AddTaskResultData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&&mView!=null){
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(AddTaskResultData addTaskResultData) {
                mView.showResult(addTaskResultData);
            }
        });
    }
    public void KsSubmitVideo(final String filePath){

        mSubscription =  dataManager.UploadVideo(filePath).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<UploadVideoData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null){

                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(UploadVideoData uploadVideoData) {
                mView.upVideoResult(uploadVideoData);
            }
        });
    }


}
